package com.gopay.bis.service.bank.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.bis.service.bank.BankIntrChannConfService;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.MerchantSettledRequest;
import com.gopay.common.domain.bank.wechat.scan.cmbc.FileDownReq;
import com.gopay.common.domain.bank.wechat.scan.cmbc.MerchantOperation;
import com.gopay.common.domain.bank.wechat.scan.cmbc.MerchantSettled;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.spring.SpringContextUtil;
import com.gopay.remote.bank.ScanPayMerchantService;
/**
 * 
 * 
 *
 * @author 宣广海
 *
 * @version 
 *
 * @since 2017年12月7日
 */
@Service("scanPayMerchantService")
public class ScanPayMerchantServiceImpl implements ScanPayMerchantService {
	
	/**
	 * 定义日志对象
	 */
	private static Logger logger = LoggerFactory.getLogger(ScanPayInteractServiceImpl.class);
	
	@Resource(name = "bankIntrChannConfService")
	private BankIntrChannConfService bankIntrChannConfService;

	@Override
	public Result merchantSettled(Object msg) throws GopayException {
		logger.error("ScanPayMerchantServiceImpl商户入驻请求参数键:" + ToStringBuilder.reflectionToString(msg));
		Result result = new Result();
		MerchantSettled request = (MerchantSettled) msg;
		String beanName=null;
		try {
			if((BisBankInterfaceChannel._17.value).equals(request.getBankInterfaceChannel())){
				beanName="WECHAT-SCAN-CMBC";
			}
			if((BisBankInterfaceChannel._25.value).equals(request.getBankInterfaceChannel())){
				beanName="WECHAT-SCAN-PAB";
			}
			ScanPayMerchantService scanPayMerchantService = (ScanPayMerchantService)SpringContextUtil.getBean(beanName);
			
			result =  (Result) scanPayMerchantService.merchantSettled(msg);
			logger.error("ScanPayMerchantServiceImpl商户入驻请求返回处理对象："+ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
	        logger.error("ScanPayMerchantServiceImpl merchantSettled error: ", e);
	        result.setResultCode(e.getErrCode());
	        result.setSuccess(false);
	    } catch (Exception e) {
	        logger.error("ScanPayMerchantServiceImpl merchantSettled error: ", e);
	        result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
	        result.setSuccess(false);
	    }
		return result;
	}

	@Override
	public Result merchantQuery(Object msg) throws GopayException {
		logger.error("ScanPayMerchantServiceImpl商户查询请求参数键:" + ToStringBuilder.reflectionToString(msg));
		Result result = new Result();
		try {
			ScanPayMerchantService scanPayMerchantService = (ScanPayMerchantService)SpringContextUtil.getBean("WECHAT-SCAN-CMBC");
			result =  (Result) scanPayMerchantService.merchantQuery(msg);
			logger.error("ScanPayMerchantServiceImpl商户查询请求返回处理对象："+ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
	        logger.error("ScanPayMerchantServiceImpl merchantSettled error: ", e);
	        result.setResultCode(e.getErrCode());
	        result.setSuccess(false);
	    } catch (Exception e) {
	        logger.error("ScanPayMerchantServiceImpl merchantQuery error: ", e);
	        result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
	        result.setSuccess(false);
	    }
		return result;
	}

	@Override
	public Result merchantModify(Object msg) throws GopayException {
		logger.error("ScanPayMerchantServiceImpl商户修改请求参数键:" + ToStringBuilder.reflectionToString(msg));
		Result result = new Result();
		try {
			ScanPayMerchantService scanPayMerchantService = (ScanPayMerchantService)SpringContextUtil.getBean("WECHAT-SCAN-CMBC");
			result =  (Result) scanPayMerchantService.merchantModify(msg);
			logger.error("ScanPayMerchantServiceImpl商户修改请求返回处理对象："+ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
	        logger.error("ScanPayMerchantServiceImpl merchantSettled error: ", e);
	        result.setResultCode(e.getErrCode());
	        result.setSuccess(false);
	    } catch (Exception e) {
	        logger.error("ScanPayMerchantServiceImpl merchantModify error: ", e);
	        result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
	        result.setSuccess(false);
	    }
		return result;
	}

	@Override
	public Result getCheckAccountFile(FileDownReq obj) throws GopayException {
		logger.error("ScanPayMerchantServiceImpl 获取对账文件请求的日期:" + obj.getDate());
		Result result = new Result();
		String beanName=null;
		try {
			if((BisBankInterfaceChannel._17.value).equals(obj.getChannel())){
				beanName="WECHAT-SCAN-CMBC";
			}
			if((BisBankInterfaceChannel._25.value).equals(obj.getChannel())){
				beanName="WECHAT-SCAN-PAB";
			}
			logger.error("beanName========"+beanName);
			ScanPayMerchantService scanPayMerchantService = (ScanPayMerchantService)SpringContextUtil.getBean(beanName);
			result =  (Result) scanPayMerchantService.getCheckAccountFile(obj);
			
		} catch (GopayException e) {
	        logger.error("ScanPayMerchantServiceImpl getCheckAccountFile error: ", e);
	        result.setResultCode(e.getErrCode());
	        result.setSuccess(false);
	    } catch (Exception e) {
	        logger.error("ScanPayMerchantServiceImpl getCheckAccountFile error: ", e);
	        result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
	        result.setSuccess(false);
	    }
		return result;
	}

	@Override
	public Result getCheckAlipayAccountFile(FileDownReq obj) throws GopayException {
		logger.error("ScanPayMerchantServiceImpl 支付宝获取对账文件请求的日期:" + obj.getDate());
		Result result = new Result();
		String beanName=null;
		try {
			if((BisBankInterfaceChannel._17.value).equals(obj.getChannel())){
				beanName="WECHAT-SCAN-CMBC";
			}
			if((BisBankInterfaceChannel._25.value).equals(obj.getChannel())){
				beanName="WECHAT-SCAN-PAB";
			}
			ScanPayMerchantService scanPayMerchantService = (ScanPayMerchantService)SpringContextUtil.getBean(beanName);
			result =  (Result) scanPayMerchantService.getCheckAlipayAccountFile(obj);
			
		} catch (GopayException e) {
	        logger.error("ScanPayMerchantServiceImpl merchantSettled error: ", e);
	        result.setResultCode(e.getErrCode());
	        result.setSuccess(false);
	    } catch (Exception e) {
	        logger.error("ScanPayMerchantServiceImpl merchantModify error: ", e);
	        result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
	        result.setSuccess(false);
	    }
		return result;
	}

	@Override
	public Result merchantOperation(Object msg) throws GopayException {
		logger.error("ScanPayMerchantServiceImpl商户配置请求参数键:" + ToStringBuilder.reflectionToString(msg));
		Result result = new Result();
		MerchantOperation request = (MerchantOperation) msg;
		String beanName=null;
		try {
			if((BisBankInterfaceChannel._17.value).equals(request.getBankInterfaceChannel())){
				beanName="WECHAT-SCAN-CMBC";
			}
			else if((BisBankInterfaceChannel._25.value).equals(request.getBankInterfaceChannel())){
				beanName="WECHAT-SCAN-PAB";
			}else{
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7022);
			}
			ScanPayMerchantService scanPayMerchantService = (ScanPayMerchantService)SpringContextUtil.getBean(beanName);
			
			result =  (Result) scanPayMerchantService.merchantOperation(msg);
			logger.error("ScanPayMerchantServiceImpl商户配置返回处理对象："+ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
	        logger.error("ScanPayMerchantServiceImpl merchantOperation error: ", e);
	        result.setResultCode(e.getErrCode());
	        result.setSuccess(false);
	    } catch (Exception e) {
	        logger.error("ScanPayMerchantServiceImpl merchantOperation error: ", e);
	        result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
	        result.setSuccess(false);
	    }
		return result;
	}

	@Override
	public Result merchantOperationQuery(Object msg) throws GopayException {
		logger.error("ScanPayMerchantServiceImpl 商户配置查询请求参数键:" + ToStringBuilder.reflectionToString(msg));
		Result result = new Result();
		try {
			ScanPayMerchantService scanPayMerchantService = (ScanPayMerchantService)SpringContextUtil.getBean("WECHAT-SCAN-CMBC");
			result =  (Result) scanPayMerchantService.merchantOperationQuery(msg);
			logger.error("ScanPayMerchantServiceImpl商户配置查询返回处理对象："+ToStringBuilder.reflectionToString(result));
		} catch (GopayException e) {
	        logger.error("ScanPayMerchantServiceImpl merchantOperationQuery error: ", e);
	        result.setResultCode(e.getErrCode());
	        result.setSuccess(false);
	    } catch (Exception e) {
	        logger.error("ScanPayMerchantServiceImpl merchantOperationQuery error: ", e);
	        result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
	        result.setSuccess(false);
	    }
		return result;
	}

}
